﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace library
{
    public partial class pubInfo : Form
    {
        public pubInfo()
        {
            InitializeComponent();
        }
        string pubId = ""; //定义全局变量，用于存储编号

        private void btn_save_Click(object sender, EventArgs e)
        {
            string pubName = pubName_box.Text.Trim();
            if (pubName == "")
            {
                MessageBox.Show("输入出版社名称");
                return;
            }
            string contact = contact_box.Text.Trim();
            if (contact == "")
            {
                MessageBox.Show("输入出版社联系人");
                return;
            }
            string contactPhone = contactPhone_box.Text.Trim();
            if (contactPhone == "")
            {
                MessageBox.Show("输入联系电话");
                return;
            }
            string fax = fax_box.Text.Trim();
            if (fax == "")
            {
                MessageBox.Show("输入传真");
                return;
            }
            string province = province_box.Text.Trim();
            if (province == "")
            {
                MessageBox.Show("输入完整地址");
                return;
            }
            string city = city_box.Text.Trim();
            if (city == "")
            {
                MessageBox.Show("输入完整地址");
                return;
            }
            string district = district_box.Text.Trim();
            if (district == "")
            {
                MessageBox.Show("输入完整地址");
                return;
            }
            string houseNum = houseNum_box.Text.Trim();
            if (houseNum == "")
            {
                MessageBox.Show("输入完整地址");
                return;
            }

            if (lbl_note.Text == "添加")
            {

                string sql = string.Format("INSERT INTO pubinfo (pubName, contact, contactPhone, fax, province, city, district, houseNum) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')", pubName,contact,contactPhone,fax,province,city,district,houseNum);
                MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int i = cmd.ExecuteNonQuery();
                conn.Close();
                if (i > 0)
                {
                    MessageBox.Show("添加出版社成功");
                    ClearTexBox(); //调用函数，清空各控件
                    DataBind_pub();
                }
                else
                {
                    MessageBox.Show("添加出版社失败");
                    return;
                }
            }
            else if (lbl_note.Text.Trim() == "修改")
            {
                MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
                conn.Open();
                
                string str = string.Format("update pubinfo set pubName = '{0}', contact = '{1}', contactPhone = '{2}', fax ='{3}',province = '{4}',city = '{5}',district = '{6}',houseNum='{7}' where pubNum = {8};", pubName, contact, contactPhone, fax, province, city, district, houseNum, pubId);
                MySqlCommand cmd = new MySqlCommand(str, conn);
                int i = cmd.ExecuteNonQuery();
                conn.Close();
                if (i > 0)
                {
                    MessageBox.Show("信息修改成功！");
                    DataBind_pub(); //重新加载客户信息
                }
                else
                {
                    MessageBox.Show("信息修改失败！");
                }
                lbl_note.Text = "添加";
                ClearTexBox();
            }
        }

        protected void ClearTexBox()
        {
            pubName_box.Text = "";
            contact_box.Text = "";
            contactPhone_box.Text = "";
            fax_box.Text = "";
            province_box.Text = "";
            city_box.Text = "";
            district_box.Text = "";
            houseNum_box.Text = "";
            pubId = "";
        }

        protected void DataBind_pub()
        {
            MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
            conn.Open();
            string str = "select * from pubinfo";
            MySqlDataAdapter da = new MySqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            conn.Close();
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem myitem = new ListViewItem(dr["pubName"].ToString());
                
                myitem.SubItems.Add(dr["contact"].ToString());
                
                myitem.SubItems.Add(dr["contactPhone"].ToString());
                myitem.SubItems.Add(dr["fax"].ToString());
                myitem.SubItems.Add(dr["province"].ToString());
                myitem.SubItems.Add(dr["city"].ToString());
                myitem.SubItems.Add(dr["district"].ToString());
                
                myitem.SubItems.Add(dr["houseNum"].ToString());

                listView1.Items.Add(myitem); //将新建项添加到 ListView 控件中
            }

        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            ClearTexBox();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0) //如果有选中项
            {
                ListViewItem myitem = listView1.SelectedItems[0]; //获取选中的第一行（一次只能选一行）
                pubName_box.Text = myitem.SubItems[0].Text; //将选中行第 1 列的值赋值全局变量，客户编号
                
                contact_box.Text = myitem.SubItems[1].Text;
                contactPhone_box.Text = myitem.SubItems[2].Text;
                fax_box.Text = myitem.SubItems[3].Text;
                province_box.Text = myitem.SubItems[4].Text;
                city_box.Text = myitem.SubItems[5].Text;
                district_box.Text = myitem.SubItems[6].Text;
                houseNum_box.Text = myitem.SubItems[7].Text;
                lbl_note.Text = "修改";
                MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
                conn.Open();
                string str = string.Format("select pubNum from pubinfo where pubName = '{0}'", pubName_box.Text.Trim());
                MySqlDataAdapter da = new MySqlDataAdapter(str, conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                foreach (DataRow dr in dt.Rows)
                {
                    pubId = dr["pubNum"].ToString();
                }
            }
        }

        private void delete_btn_Click(object sender, EventArgs e)
        {
            if (pubId == "") //如果没有选中要删除的客户信息
            {
                MessageBox.Show("请先选择要删除的出版社信息");
            }
            else
            {
                //弹出删除确认提示框
                DialogResult result = MessageBox.Show("确定要删除选中的出版社信息吗？", "删除提示",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes) //如果确定删除
                {
                    MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
                    conn.Open();
                    string str = string.Format("delete from pubinfo where pubNum ={0}", pubId);
                    MySqlCommand cmd = new MySqlCommand(str, conn);
                    int i = cmd.ExecuteNonQuery();
                    conn.Close();
                    if (i > 0)
                    {
                        MessageBox.Show("删除成功");
                        ClearTexBox();
                        DataBind_pub();
                    }
                    else
                    {
                        MessageBox.Show("出版社信息删除失败！");
                    }
                }
            }
        }

        private void pubInfo_Load(object sender, EventArgs e)
        {
            DataBind_pub();
        }

        private void return_btn_Click(object sender, EventArgs e)
        {
            this.Hide();
            Main frm = new Main();
            frm.Show();
        }
    }
}
